<?php
/**
 * @version SVN: $Id: view.html.php 1222 2010-06-04 14:41:15Z elkuku $
 * @package    EasyCreator
 * @subpackage Views
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath (elkuku) {@link http://www.nik-it.de NiK-IT.de}
 * @author     Created on 07-Mar-2008
 * @license    GNU/GPL, see JROOT/LICENSE.php
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the EasyCreator Component
 *
 * @package EasyCreator
 * @subpackage Views
 */
class EasyCreatorViewStuffer extends JView
{
    protected $plumbing = true;

    public function display($tpl = null)
    {
        ecrScript('stuffer');

        $this->ecr_project = JRequest::getCmd('ecr_project');

        $this->project = EasyProjectHelper::getProject($this->ecr_project, true);

        if( ! $this->project)
        {
            //-- Something went wrong..
            JError::raiseWarning(100, JText::sprintf('Unable to load the project %s', $this->ecr_project));
            ecrHTML::easyFormEnd();

            return;
        }

        $task = JRequest::getCmd('task', 'stuffer');
        $tmpl = JRequest::getCmd('tmpl');

        if($task != 'display_snip'
        && $task != 'aj_reflection'
        && $tmpl != 'component' )
        {
            //--Draw h1 header
            ecrHTML::header(JText::_('Configure'), $this->project);

            //--Draw the submenu if task is not for a raw view
            echo $this->displayBar($task);
        }

        if(in_array($task, get_class_methods($this)))
        {
            //--Execute the task
            $this->$task();

            if($task == 'display_snip')
            {
                //--Raw view
                parent::display($tpl);
                return;
            }
        }
        else
        {
            if($task) echo 'UNDEFINED..'.$task.'<br />';
            $this->stuffer();
        }

        $this->assignRef('task', $task);
        $this->assignRef('tmpl', $tmpl);

        parent::display($tpl);

        ecrHTML::easyFormEnd();
    }//function

    /**
     * Displays the submenu
     *
     * @param $task the actual task
     * @return string html
     */
    private function displayBar($task)
    {
        $ecr_help = JComponentHelper::getParams('com_easycreator')->get('ecr_help');

        $subtasks = array(
        array('title' => JText::_('Building')
        , 'description' => JText::_('DESC PROJECT BUILDING')
        , 'icon' => 'config'
        , 'task' => 'stuffer'
        )
        , array('title'=> JText::_('Files')
        , 'description' => JText::_('DESC PROJECT FILES')
        , 'icon' => 'directory'
        , 'task' => 'files'
        )
        , array('title'=> JText::_('Installing')
        , 'description' => JText::_('DESC PROJECT INSTALLING')
        , 'icon' => 'install'
        , 'task' => 'install'
        )
        , array('title' => JText::_('Parameters')
        , 'description' => JText::_('DESC PROJECT PARAMETERS')
        , 'icon' => 'article'
        , 'task' => 'projectparams'
        )
        , array('title' => JText::_('DataDesigner')
        , 'description' => JText::_('DESC PROJECT DATADESIGNER')
        , 'icon' => 'db'
        , 'task' => 'tables'
        )
        , array('title' => JText::_('Remove Project')
        , 'description' => JText::_('DESC PROJECT DELETE')
        , 'icon' => 'logout'
        , 'task' => 'projectdelete'
        )
        );

        $htmlDescriptionDivs = '';
        $jsVars = '';
        $jsMorphs = '';
        $jsEvents = '';
        $html = '';

        if($task == 'stuffer'
        || $task == 'install')
        {
            $html .= '<div class="ecr_easy_toolbar" style="float: right;">';
            $html .= '	<div class="ecr_button img icon-16-save" onclick="submitform(\'save_config\');">'.JText::_('Save').'</div>';
            $html .= '</div>';
        }

        $html .= '<div id="ecr_sub_toolbar" style="margin-bottom: 1em; margin-top: 0.5em;">';

        foreach($subtasks as $sTask)
        {
            if($this->project->type != 'component'
            && ($sTask['task'] == 'tables'
            || $sTask['task'] == 'install')
            )
            {
                continue;
            }

            if($this->project->type == 'package')
            {
                if($sTask['task'] != 'stuffer'
                && $sTask['task'] != 'projectdelete')
                continue;
            }

            $selected =($sTask['task'] == $task) ? '_selected' : '';
            $html .= '<span id="btn_'.$sTask['task'].'" style="margin-left: 0.3em;" class="ecr_button'.$selected.' img icon-16-'.$sTask['icon'].'" onclick="submitbutton(\''.$sTask['task'].'\');">';
            $html .= $sTask['title'].'</span>';

            if($ecr_help == 'all'
            || $ecr_help == 'some')
            {
                $htmlDescriptionDivs .= '<div class="hidden_div ecr_description" id="desc_'.$sTask['task'].'">'.$sTask['description'].'</div>';
                $jsVars .= "var desc_".$sTask['task']." = $('desc_".$sTask['task']."');\n";

                $jsEvents .= "$('btn_".$sTask['task']."').addEvents({\n"
                . "'mouseenter': showTaskDesc.bind(desc_".$sTask['task']."),\n"
                . "'mouseleave': hideTaskDesc.bind(desc_".$sTask['task'].")\n"
                . "});\n";
            }
        }//foreach


        $html .= $htmlDescriptionDivs;

        if( $ecr_help == 'all'
        || $ecr_help == 'some')
        {
            $html .= "<script type='text/javascript'>"
            ."window.addEvent('domready', function() {\n"
            ."function showTaskDesc(name) {\n"
            ."this.setStyle('display', 'block');\n"
            ."}\n"
            ."function hideTaskDesc(name) {\n"
            ."	this.setStyle('display', 'none');\n"
            ."}\n"
            . $jsVars
            . $jsEvents
            . "});\n"
            . "</script>";
        }

        $html .= '</div>';

        return $html;
    }//function

    private function stuffer()
    {
        $this->projectList = EasyProjectHelper::getProjectList();
        $this->installFiles = EasyProjectHelper::findInstallFiles($this->project);

        $this->setLayout('stuffer');
    }// functionconfig

    private function files()
    {
        ecrLoadHelper('file');
        $this->easyFile = new EasyFile();

        ecrScript('addelement');
        $this->setLayout('files');
    }//function

    private function install()
    {
        $this->installFiles = EasyProjectHelper::findInstallFiles($this->project);

        $this->setLayout('install');
    }//function

    private function projectdelete()
    {
        $this->setLayout('deleteconfirm');
    }//function

    private function projectparams()
    {
        $selected_xml = JRequest::getVar('selected_xml');
        $params = array();
        $xmlFiles = array();

        foreach($this->project->copies as $path)
        {
            if(JFolder::exists($path))
            {
                $files = JFolder::files($path,'\.xml', true, true);

                if(count($files))
                {
                    foreach($files as $file)
                    {
                        $xmlFiles[] = substr($file, strlen(JPATH_ROOT) + 1);
                    }//foreach
                }
            }
            else if(JFile::getExt($path) == 'xml')
            {
                $xmlFiles[] = substr($path, strlen(JPATH_ROOT) + 1);
            }
        }//foreach

        if(in_array($selected_xml, $xmlFiles))
        {
            //--Get the project params
            $params = new JParameter('', JPATH_ROOT.DS.$selected_xml);
        }

        $this->assignRef('params', $params);
        $options = array();
        $options[] =  JHTML::_('select.option', '',JText::_('Select'));

        for ($i=1; $i < sizeof($xmlFiles) + 1; $i++)
        {
            $options[$i] = JHTML::_('select.option', $xmlFiles[$i-1]);
        }//for

        $xmlSelector = JHTML::_('select.genericlist', $options, 'selected_xml', 'style="font-size: 1.3em;" onchange="submitbutton(\''.JRequest::getCmd('task').'\');"', 'value', 'text', $selected_xml);
        $this->assignRef('xmlSelector', $xmlSelector);

        $this->assignRef('selected_xml', $selected_xml);
        $this->setLayout('projectparams');
    }//function

    private function table_edit()
    {
        $this->plumbing = false;
        $this->tables();
    }//function

    private function tables()
    {
        ecrScript('addelement');

        $this->setLayout('tables');
    }//function

    private function register_table()
    {
        $this->tables();
    }//function

    private function createTable()
    {
        $this->setLayout('tables');
    }//function

    private function display_snip()
    {
        $path = JRequest::getVar('file_path');
        $start = JRequest::getInt('start');
        $end = JRequest::getInt('end');

        if( ! JFile::exists($path))
        {
            echo '<div class="ebc_error" align="center">'.JText::_('File not found').'</div>';
            echo $path;

            //--EXIT
            jexit();
        }
        else
        {
            $fileContents = JFile::read($path);
        }

        if($fileContents)
        {
            $fileContents = explode("\n", $fileContents);

            $this->assignRef('fileContents', $fileContents);
            $this->assignRef('startAtLine', $start);
            $this->assignRef('endAtLine', $end);
            $this->assignRef('path', $path);
        }

        $this->setLayout('snippet');
    }//function

}// class
